package controller.admin;

import model.Goods;
import service.GoodsService;
import utils.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 商品编辑
 */
@WebServlet(name = "AdminGoodsEditServlet", value = "/admin/goods_edit")
@MultipartConfig()
public class AdminGoodsEditServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //图片存的位置
        String directory = "picture" + "/";
        //返回web应用程序文档根目录
        String path = this.getServletContext().getRealPath("/" + directory);

        //获取参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String intro = request.getParameter("intro");
        String stock = request.getParameter("stock");
        String typeid = request.getParameter("typeid");
        String pageNumber = request.getParameter("pageNumber");
        String type = request.getParameter("type");

        //获取上传的文件
        Part cover = request.getPart("cover");
        Part image1 = request.getPart("image1");
        Part image2 = request.getPart("image2");

        GoodsService gs = new GoodsService();
        Goods goodsTemp = null;
        //必填项
        if (!(name != null && !"".equals(name))) {
            response.sendRedirect(request.getHeader("Referer"));
            return;
        }

        //id不为空
        if (id != null && !"".equals(id)) {
            try {
                goodsTemp = gs.goods_detail(Integer.parseInt(id));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            response.sendRedirect(request.getHeader("Referer"));
            return;
        }

        //没有获取到id对应的商品就返回
        if (goodsTemp == null) {
            response.sendRedirect(request.getHeader("Referer"));
            return;
        }

        //封装数据
        Goods goods = new Goods();
        goods.setId(Integer.parseInt(id));
        goods.setName(name);
        goods.setIntro(intro);
        if (price != null && !"".equals(price)) goods.setPrice(Float.parseFloat(price));
        if (stock != null && !"".equals(stock)) goods.setStock(Integer.parseInt(stock));
        if (typeid != null && !"".equals(typeid)) goods.setTypeid(Integer.parseInt(typeid));

        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }

        //文件名 UUID后几位
        String coverName = FileUtils.getShortUUID(cover);
        String image1Name = FileUtils.getShortUUID(image1);
        String image2Name = FileUtils.getShortUUID(image2);

        if (coverName != null) {
            //写数据
            cover.write(path + coverName);
            //封装新传入的数据数据
            goods.setCover(directory + coverName);
        } else {
            //没有传文件的话封装原先的数据
            goods.setCover(goodsTemp.getCover());
        }

        if (image1Name != null) {
            image1.write(path + image1Name);
            goods.setImage1(directory + image1Name);
        } else {
            goods.setImage1(goodsTemp.getImage1());
        }

        if (image2Name != null) {
            image2.write(path + image2Name);
            goods.setImage2(directory + image2Name);
        } else {
            goods.setImage2(goodsTemp.getImage2());
        }

        String path1 = this.getServletContext().getRealPath("/");
        String desPathStr1 = "D:" + File.separator + "DOME\\文档\\Practice\\Java Web\\蛋糕商场\\web\\";

        //如果前后文件名不同说明上传了文件  需要把之前的删除
        if (!goodsTemp.getCover().equals(goods.getCover())) {
            FileUtils.deleteFile(path1 + goodsTemp.getCover());
            FileUtils.deleteFile(desPathStr1 + goodsTemp.getCover());
        }
        if (!goodsTemp.getImage1().equals(goods.getImage1())) {
            FileUtils.deleteFile(path1 + goodsTemp.getImage1());
            FileUtils.deleteFile(desPathStr1 + goodsTemp.getImage1());
        }
        if (!goodsTemp.getImage2().equals(goods.getImage2())) {
            FileUtils.deleteFile(path1 + goodsTemp.getImage2());
            FileUtils.deleteFile(desPathStr1 + goodsTemp.getImage2());
        }

        //更新商品
        boolean flag = gs.updateGoods(goods);

        //如果更新成功就拷贝一份到源代码目录
        if (flag) {
            //保存一份上传文件到自己的源码目录
            String desPathStr = "D:" + File.separator + "DOME\\文档\\Practice\\Java Web\\蛋糕商场\\web\\";
            FileUtils.copyFile(path1 + goods.getCover(), desPathStr);
            FileUtils.copyFile(path1 + goods.getImage1(), desPathStr);
            FileUtils.copyFile(path1 + goods.getImage2(), desPathStr);
        }
        response.sendRedirect("goods_list?pageNumber=" + pageNumber + "&" + "type" + type);
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

}
